﻿using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Web;
using System.Web.Services;
using System.Xml.XPath;
using System.Collections.Generic;


namespace GetWebInfo
{
    class ParsePage
    {
        public static bool ParseYahooSharePricePage(string pageInfo, SharePrice sp)
        { 
            string nullstr = "<tbody>\n  </tbody>";
            if (pageInfo.Contains(nullstr))
            {
                //证明没有内容了
                return false;
            }

            DateTime _dt = DateTime.Now;
            decimal _price = 0;

            string[] strs1 = pageInfo.Split(new string[] { "<tbody>\n", "</tbody>" }, StringSplitOptions.RemoveEmptyEntries);
            string[] strs2 = strs1[1].Split(new string[] { "<tr class=\"hover\">", "<tr>" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string _fstr in strs2)
            {
                if (_fstr == " ")
                    continue;
                string[] strs3 = _fstr.Split(new string[] { "<td>", "</td>" }, StringSplitOptions.RemoveEmptyEntries);

                if (!sp.Prices.ContainsKey(DateTime.Parse(strs3[0])))
                {
                    sp.Prices.Add(DateTime.Parse(strs3[0]), decimal.Parse(strs3[4]));
                }
            }           

            return true;
        }
        
        public static IndustrialStatements Parse_Reconciliation_Of_Net_Cash_Flow_From_Operating_Activities(string pageInfo, IndustrialStatements cbs)
        {
            List<string> _sourceStrs = new List<string>();
            AddSourceStrs2(pageInfo, _sourceStrs);
            
            Type myType = typeof(IndustrialStatements);
            FieldInfo[] myFields = myType.GetFields(BindingFlags.Public | BindingFlags.Instance);

            for (int i = 0; i < myFields.Length; i++)
            {
                StatementsAttribute obsAttr = (StatementsAttribute)Attribute.GetCustomAttribute(myFields[i], typeof(StatementsAttribute));

                foreach (string ts in _sourceStrs)
                {
                    if (ts.StartsWith(obsAttr.CHSName) &&
                        obsAttr.Type == StatementType.Other)
                    {
                        string sts = ts.Replace(obsAttr.CHSName, "");
                        if (!Global.IsValueFormat(sts))
                        {
                            break;
                        }

                        if (sts == "" || sts == "--")
                        {
                            myFields[i].SetValue(cbs, (decimal)0);
                            Console.WriteLine("key: [{0}] value: [{1}]", obsAttr.CHSName, 0);
                            break;
                        }
                        else
                        {
                            //sts = sts.Substring(0, sts.Length - (sts.Length - sts.IndexOf(".")));
                            myFields[i].SetValue(cbs, decimal.Parse(sts));
                            Console.WriteLine("key: [{0}] value: [{1}]", obsAttr.CHSName, sts);
                            break;
                        }
                    }
                }
            }

            return cbs;
        }

        public static void AddSourceStrs2(string pageInfo, List<string> sourceStrs)
        {
            //所有页面去废物
            Dictionary<string, string> strpairs = new Dictionary<string, string>();
            strpairs.Add("加：", "");
            strpairs.Add("（收益以“－”号填列）", "");
            strpairs.Add("(收益以“－”号填列)", "");            
            strpairs.Add("（增加以“－”号填列）", "");
            strpairs.Add("(增加以“－”号填列)", "");
            strpairs.Add("（减少以“－”号填列）", "");
            strpairs.Add("(减少以“－”号填列)", "");
            strpairs.Add("（收益以“－ ”号填列）", "");
            //
            strpairs.Add(",", "");
            strpairs.Add("\r", "");
            //strpairs.Add(" ", "");

            pageInfo = Global.StringReplaces(pageInfo, strpairs);

            string[] sourceInfo = pageInfo.Split(new char[] { '\n' });

            if (sourceStrs == null)
            {
                sourceStrs = new List<string>();
            }

            foreach (string str in sourceInfo)
            {
                string[] _tstr = str.Split(new char[] { ' ' });
                if (_tstr.Length == 3)
                {
                    sourceStrs.Add(_tstr[0] + _tstr[1]);
                }
                else
                {
                    string _intstr = str.Replace(" ", "");
                    sourceStrs.Add(_intstr);
                }
            }
        }
    }
}

